package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.webSite.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {

    @Reference
    private IUserInfoService userService;

    @RequestMapping("/checkPhone")
    public Object checkPhone(String phone) {
        return !userService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    public AjaxResult sendVerifyCode(String phone) {
        userService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userRegist")
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    public Object userLogin(HttpServletResponse response, String username, String password) {
        String token = userService.userLogin(username, password);
        //把返回的token存进cookie里响应回浏览器
        CookieUtil.addCookie(response, "token", token, RedisKeys.USER_LOGIN_TOKEN.getTimeOut().intValue());
        return  AjaxResult.SUCCESS;
    }

}
